<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2019-11-12
 * Time: 20:13
 */

namespace app\api\service;
use app\api\model\GfActivityOrder;
use app\api\model\MemberOrder;
use app\api\model\SjActivityOrder;
use app\api\model\UserAuthOrder;
use app\api\model\UserCurrencyOrder;
use app\api\service\activity_order\GfActivitySer;
use app\api\service\activity_order\SjActivitySer;
use app\api\service\order\MemberOrderSer;
use app\api\service\order\UserCurrencySer;


class WxNotify extends \WxPayNotify
{
    public function NotifyProcess($objData, $config, &$msg)
    {
       if($objData['result_code'] == 'SUCCESS' && $objData['return_code'] == 'SUCCESS')
        {

            $outTradeNo = $objData['out_trade_no'];
            //获取通知的数据
            if (empty($outTradeNo)) {
                # 如果没有数据，直接返回失败
                return false;
            }
            /**
             * 根据订单类型进行操作
             */
            $outTradeNoArr = explode("t",$outTradeNo);
            $type = $outTradeNoArr[0];
            if($type == 1) {
                // 会员卡支付订单
                return $this->updateMemberOrder($outTradeNo);
            } elseif($type == 2) {
                // 甜茶币支付订单
                return $this->updateCurrencyOrder($outTradeNo);
            } elseif($type == 3) {
                // 官方活动支付
                return $this->updateGfActivityOrder($outTradeNo);
            } elseif($type == 4) {
                // 商家活动支付
                return $this->updateSjActivityOrder($outTradeNo);
            }  elseif($type == 5) {
                // 用户认证支付
                return $this->updateUserAuthOrder($outTradeNo);
            }
        }
        else{
            return true;
        }
    }

    /**
     * 会员卡支付订单
     */
    private function updateMemberOrder($outTradeNo)
    {
        $order = MemberOrder::where('out_trade_no',$outTradeNo)->find();
        $MemberOrderSer = new MemberOrderSer();
        return $MemberOrderSer->entrance($order);
    }
    
    /**
     * 甜茶币支付订单
     */
    private function updateCurrencyOrder($outTradeNo)
    {
        $order = UserCurrencyOrder::where('out_trade_no',$outTradeNo)->find();
        $currencyOrderSer = new UserCurrencySer();
        return $currencyOrderSer->entrance($order);
    }

    /**
     * 官方活动支付订单
     */
    private function updateGfActivityOrder($outTradeNo)
    {
        $order = GfActivityOrder::where('out_trade_no',$outTradeNo)->find();
        $gfActivityOrderSer = new GfActivitySer();
        return $gfActivityOrderSer->entrance($order);
    }

    /**
     * 商家活动支付
     */
    private function updateSjActivityOrder($outTradeNo)
    {
        $order = SjActivityOrder::where('out_trade_no',$outTradeNo)->find();
        $SjActivitySer = new SjActivitySer();
        return $SjActivitySer->entrance($order);
    }

    /**
     * 用户认证订单
     */
    private function updateUserAuthOrder($outTradeNo)
    {
        $order = UserAuthOrder::where('out_trade_no',$outTradeNo)->find();
        $SjActivitySer = new SjActivitySer();
        return $SjActivitySer->entrance($order);
    }
}